//
//  FPCustomTextView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/15.
//

#import "FPCustomTextView.h"

@interface FPCustomTextView () <UITextViewDelegate>

//@property(strong , nonatomic) UILabel *tipLab;

@end

@implementation FPCustomTextView

- (void)setBackColor:(UIColor *)backColor {
    
    _backColor = backColor;
    
//    self.tintColor = backColor;
    self.backgroundColor = backColor;
}

//- (UILabel *)tipLab {
//
//    if (!_tipLab) {
//
//        _tipLab = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-100.f-8.f, self.frame.size.height - 17.f - 8.f, 100.f, 17.f)];
//        _tipLab.backgroundColor = UIColor.clearColor;
//        _tipLab.textAlignment = NSTextAlignmentRight;
//        _tipLab.font = FONT(14.f);
//        _tipLab.textColor = UIColor.lightGrayColor;
//        _tipLab.hidden = YES;
//        [self addSubview:_tipLab];
//    }
//    return _tipLab;
//}

//- (void)setMaxNum:(NSInteger)maxNum {
//
//    _maxNum = maxNum;
//    _tipLab.hidden = NO;
//
//    self.tipLab.text = [NSString stringWithFormat:@"0/%zd",self.maxNum];
//}

//- (void)textViewDidChange:(UITextView *)textView {
//
//    if (self.maxNum>0) {
//
//        self.tipLab.text = [NSString stringWithFormat:@"%zd/%zd", textView.text.length ,self.maxNum];
//    }
//}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//
//    if (self.maxNum > 0) {
//
//        NSInteger existedLength = textView.text.length;
//        NSInteger selectedLength = range.length;
//        NSInteger replaceLength = text.length;
//
//        if (existedLength - selectedLength + replaceLength > self.maxNum) {
//
//            return NO;
//        }
//        else {
//
//            return YES;
//        }
//    }
//
//    return YES;
//}



- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.tintColor = [UIColor hexColor:@"#29B1A3"];
//    self.backgroundColor = UIColorHex(#F5F7F6);
    
//    self.maxNum = 0;
    CGFloat xMargin = 0.f;
    CGFloat yMargin = 10.f;
    // 使用textContainerInset设置top、left、right
    self.textContainerInset = UIEdgeInsetsMake(yMargin, xMargin, 0, xMargin);
    //当光标在最后一行时，始终显示低边距，需使用contentInset设置bottom.
    self.contentInset = UIEdgeInsetsMake(0, 0, yMargin, 0);
    //防止在拼音打字时抖动
    self.layoutManager.allowsNonContiguousLayout = NO;
    
    self.textColor = UIColor.blackColor;
    self.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
//        self.maxNum = 0;
        self.tintColor = [UIColor hexColor:@"#29B1A3"];
//        self.backgroundColor = UIColorHex(#F5F7F6);
        
        CGFloat xMargin = 0.f;
        CGFloat yMargin = 10.f;
        // 使用textContainerInset设置top、left、right
        self.textContainerInset = UIEdgeInsetsMake(yMargin, xMargin, 0, xMargin);
        //当光标在最后一行时，始终显示低边距，需使用contentInset设置bottom.
        self.contentInset = UIEdgeInsetsMake(0, 0, yMargin, 0);
        //防止在拼音打字时抖动
        self.layoutManager.allowsNonContiguousLayout = NO;
        
        self.textColor = UIColor.blackColor;
        self.delegate = self;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
    }
    
    return self;
}


- (void)textDidChange:(NSNotification *)notification {
    
    [self setNeedsDisplay];
}


- (void)setPlaceHolder:(NSString *)placeHolder {

    _placeHolder = placeHolder;
    self.placeHolderLabel.text = placeHolder;
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    
    [super setText:text];
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font {
    
    [super setFont:font];
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {
    
    // 如果有输入文字，就直接返回，不画占位文字
    if (self.hasText) {
     
        self.placeHolderLabel.hidden = YES;
    }
    else {
        
        self.placeHolderLabel.hidden = NO;
        self.placeHolderLabel.text = _placeHolder;
    }
    
//    self.textColor = UIColorHex(#29B1A3);

//    //设置文字属性
//    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
//    attributes[NSFontAttributeName] = self.font;
//    attributes[NSForegroundColorAttributeName] = self.placeHolder ? self.placeHolder : [UIColor lightGrayColor];
//    //画文字
//    CGFloat x = 5;
//    CGFloat width = rect.size.width - 2 * x;
//    CGFloat y = 8;
//    CGFloat height = rect.size.height - 2 * y;
//    CGRect placeholderRect = CGRectMake(x, y, width, height);
//    [self.placeHolder drawInRect:placeholderRect withAttributes:attributes];
}


- (UILabel *)placeHolderLabel {
    
    if (_placeHolderLabel == nil) {
        
        _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.f, 0.f, self.width - 48.f, 36.f)];
        [self addSubview:_placeHolderLabel];
        _placeHolderLabel.font = self.font;
        _placeHolderLabel.numberOfLines = 0;
        _placeHolderLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _placeHolderLabel.textColor = [UIColor hexColor:@"#999999"];
    }
    
    return _placeHolderLabel;
}


- (void)dealloc {
    
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
